package com.zhuss.idea.processor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.idea.idea.wechat.DelTopicIdeaBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.WpsException;
import com.zhuss.idea.db.dao.ext.IdeaTopicDao;
import com.zhuss.util.JsonUtil;

/**
 * 删除评论奇思妙想 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/28 16:01
 */
@Component("ideaDelTopic")
public class IdeaDelTopic extends AbstractProcessor<Object, DelTopicIdeaBo> implements Processor<Object, DelTopicIdeaBo> {

    @Autowired
	@Qualifier("ideaRead")
	private IdeaRead ideaRead;
    
    @Autowired
	@Qualifier("ideaTopicRead")
	private IdeaTopicRead ideaTopicRead;
    
    @Autowired
    private IdeaTopicDao ideaTopicDao;
    
    /**
     * 校验
     */
    @Override
    public void check(DelTopicIdeaBo bo) throws WpsException {
    	
    }
    
    /**
     * 操作方法
     * @throws WpsException 
     */
    @Override
    public Object process(DelTopicIdeaBo param) throws WpsException {
    	Long ideaTopicId = param.getIdeaTopicId();
    	List<Long> ideaTopicIdList = param.getIdeaTopicIdList();
    	ideaTopicIdList = new ArrayList<Long>();
    	ideaTopicIdList.add(ideaTopicId);
    	param.setIdeaTopicIdList(ideaTopicIdList);
    	batchProcess(param);
    	param.setName(ideaTopicId + "");
    	return null;
    }
    
    private void batchProcess(DelTopicIdeaBo param) throws WpsException {
    	List<Long> ideaTopicIdList = param.getIdeaTopicIdList();
    	
    	if (ideaTopicIdList == null || ideaTopicIdList.isEmpty()) {
    		return;
    	}
    	
    	Integer deleted = param.getDeleted();
    	Long modifierId = param.getConcurrentUserId();
    	String modifier = param.getConcurrentUserName();
    	Date modifyTime = new Date();
    	
    	List<Long> ideaTopicSonList = ideaTopicRead.listSon(ideaTopicIdList);
    	DelTopicIdeaBo sonBo = new DelTopicIdeaBo();
    	BeanUtils.copyProperties(param, sonBo);
    	if (ideaTopicSonList != null && !ideaTopicSonList.isEmpty()) {
    		sonBo.setIdeaTopicIdList(ideaTopicSonList);
    	} else {
    		sonBo.setIdeaTopicIdList(null);
    	}
    	batchProcess(sonBo);
    	ideaTopicDao.deleteByParentBatch(ideaTopicIdList, deleted, modifierId, modifier, modifyTime);
    	ideaTopicDao.deleteBatch(ideaTopicIdList, deleted, modifierId, modifier, modifyTime);
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(DelTopicIdeaBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.奇思妙想.key());
    	record.setAction(LogActionEnum.奇思妙想删除评论.key());
    	record.setTargetId(param.getIdeaTopicId());
    	record.setTargetName("评论内容：" + param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
